﻿using Koala.Pro.Common;
using Koala.Pro.Common.Extension;
using Koala.Pro.Core;
using Koala.Pro.Core.Dto;
using Koala.Pro.Core.Models;
using SqlSugar;

namespace Koala.Pro.Services.Role
{
    public class RoleDataService : MyCrudService<RoleDataInfo, string, RoleDataPagedDto>, IRoleDataService, IMyCrudService<RoleDataInfo, string, RoleDataPagedDto>, ITransientDependency, IDependency
    {
        private IRoleService _roleService;

        private IUserService _userService;

        public RoleDataService(IApiUserSession currentApiUser, IRoleService roleService, IUserService userService)
        {
            base.CurrentApiUser = currentApiUser;
            _roleService = roleService;
            _userService = userService;
        }

        public override Task<Dictionary<string, string>> GetColumnNameAlias()
        {
            return Task.FromResult(new Dictionary<string, string>
        {
            { "Role_ID", "角色ID" },
            { "BelongCompanys", "所属公司" },
            { "BelongDepts", "所属部门" },
            { "ExcludeDepts", "排除部门" },
            { "Note", "备注" }
        });
        }

        protected override ISugarQueryable<RoleDataInfo> CreateFilteredQueryAsync(RoleDataPagedDto input)
        {
            return base.CreateFilteredQueryAsync(input).WhereIF(!input.ExcludeId.IsNullOrWhiteSpace(), (RoleDataInfo t) => t.Id != input.ExcludeId).WhereIF(input.Role_ID.HasValue, (RoleDataInfo s) => (int?)s.Role_ID == input.Role_ID)
                .WhereIF(!input.BelongCompanys.IsNullOrWhiteSpace(), (RoleDataInfo t) => t.BelongCompanys.Contains(input.BelongCompanys))
                .WhereIF(!input.BelongDepts.IsNullOrWhiteSpace(), (RoleDataInfo t) => t.BelongDepts.Contains(input.BelongDepts))
                .WhereIF(!input.ExcludeDepts.IsNullOrWhiteSpace(), (RoleDataInfo t) => t.ExcludeDepts.Contains(input.ExcludeDepts))
                .WhereIF(!input.Note.IsNullOrWhiteSpace(), (RoleDataInfo t) => t.Note.Contains(input.Note));
        }

        protected override ISugarQueryable<RoleDataInfo> ApplyDefaultSorting(ISugarQueryable<RoleDataInfo> query)
        {
            return query.OrderBy((RoleDataInfo s) => s.Id);
        }

        public async Task<RoleDataInfo> FindByRoleId(int roleId)
        {
            return await GetFirstAsync((RoleDataInfo s) => s.Role_ID == roleId);
        }

        public async Task<Dictionary<int, int>> GetRoleDataDict(int roleID)
        {
            Dictionary<int, int> dict = new Dictionary<int, int>();
            RoleDataInfo roleDataInfo = await FindByRoleId(roleID);
            if (roleDataInfo != null)
            {
                if (!string.IsNullOrEmpty(roleDataInfo.BelongCompanys))
                {
                    foreach (int item in roleDataInfo.BelongCompanys.ToDelimitedList<int>(","))
                    {
                        if (!dict.ContainsKey(item))
                        {
                            dict.Add(item, item);
                        }
                    }
                }
                if (!string.IsNullOrEmpty(roleDataInfo.BelongDepts))
                {
                    foreach (int item2 in roleDataInfo.BelongDepts.ToDelimitedList<int>(","))
                    {
                        if (!dict.ContainsKey(item2))
                        {
                            dict.Add(item2, item2);
                        }
                    }
                }
            }
            return dict;
        }

        public async Task<bool> UpdateRoleData(int roleId, string belongCompanys, string belongDepts)
        {
            RoleDataInfo roleDataInfo = await FindByRoleId(roleId);
            bool result;
            if (roleDataInfo != null)
            {
                roleDataInfo.BelongCompanys = belongCompanys;
                roleDataInfo.BelongDepts = belongDepts;
                result = await UpdateAsync(roleDataInfo);
            }
            else
            {
                roleDataInfo = new RoleDataInfo();
                roleDataInfo.Role_ID = roleId;
                roleDataInfo.BelongCompanys = belongCompanys;
                roleDataInfo.BelongDepts = belongDepts;
                result = await InsertAsync(roleDataInfo);
            }
            return result;
        }

        public async Task<List<int>> GetBelongCompanysByUser(int userId)
        {
            List<RoleDataInfo> obj = await FindByUser(userId);
            List<int> list = new List<int>();
            foreach (RoleDataInfo item in obj)
            {
                if (string.IsNullOrEmpty(item.BelongCompanys))
                {
                    continue;
                }
                foreach (int item2 in item.BelongCompanys.ToDelimitedList<int>(","))
                {
                    if (!list.Contains(item2))
                    {
                        list.Add(item2);
                    }
                }
            }
            return list;
        }

        public async Task<List<int>> GetBelongDeptsByUser(int userId)
        {
            List<RoleDataInfo> obj = await FindByUser(userId);
            List<int> list = new List<int>();
            foreach (RoleDataInfo item in obj)
            {
                if (string.IsNullOrEmpty(item.BelongDepts))
                {
                    continue;
                }
                foreach (int item2 in item.BelongDepts.ToDelimitedList<int>(","))
                {
                    if (!list.Contains(item2))
                    {
                        list.Add(item2);
                    }
                }
            }
            return list;
        }

        public async Task<List<RoleDataInfo>> FindByUser(int userId)
        {
            List<RoleInfo> obj = await _roleService.GetRolesByUser(userId);
            List<int> roleList = new List<int>();
            foreach (RoleInfo item in obj)
            {
                roleList.Add(item.Id);
            }
            UserInfo userInfo = await _userService.GetAsync(userId);
            List<RoleDataInfo> list = new List<RoleDataInfo>();
            foreach (int item2 in roleList)
            {
                RoleDataInfo roleDataInfo = await FindByRoleId(item2);
                if (roleDataInfo == null)
                {
                    continue;
                }
                if (!string.IsNullOrEmpty(roleDataInfo.BelongCompanys))
                {
                    List<int> list2 = new List<int>();
                    List<int> list3 = roleDataInfo.BelongCompanys.ToDelimitedList<int>(",");
                    for (int i = 0; i < list3.Count; i++)
                    {
                        if (list3[i] == -1)
                        {
                            list3[i] = userInfo.Company_ID.ToInt32();
                        }
                        if (!list2.Contains(list3[i]))
                        {
                            list2.Add(list3[i]);
                        }
                    }
                    roleDataInfo.BelongCompanys = string.Join(",", list2);
                }
                if (!string.IsNullOrEmpty(roleDataInfo.BelongDepts))
                {
                    List<int> list4 = new List<int>();
                    List<int> list5 = roleDataInfo.BelongDepts.ToDelimitedList<int>(",");
                    for (int j = 0; j < list5.Count; j++)
                    {
                        if (list5[j] == -11)
                        {
                            list5[j] = userInfo.Dept_ID.ToInt32();
                        }
                        if (!list4.Contains(list5[j]))
                        {
                            list4.Add(list5[j]);
                        }
                    }
                    roleDataInfo.BelongDepts = string.Join(",", list5);
                }
                list.Add(roleDataInfo);
            }
            return list;
        }
    }
}
